package org.shijiagen.manager.controller;

import lombok.RequiredArgsConstructor;
import org.shijiagen.manager.service.ProductService;
import org.shijiagen.manager.utils.QiNiuOSSUtils;
import org.shijiagen.param.ProductSaveParam;
import org.shijiagen.param.ProductSearchParam;
import org.shijiagen.pojo.Product;
import org.shijiagen.utils.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    private final QiNiuOSSUtils qiNiuOSSUtils;

    @GetMapping("list")
    public R adminList(ProductSearchParam param){
        return productService.search(param);
    }

    @PostMapping("upload")
    public R adminUpload(MultipartFile img) throws IOException {
        String fileName = UUID.randomUUID().toString().replaceAll("-","") + img.getOriginalFilename();
        String imgUrl = qiNiuOSSUtils.uploadImage(fileName, img.getBytes(), img.getContentType());
        return R.ok("图片上传成功",imgUrl);
    }

    @PostMapping("save")
    public R adminSave(ProductSaveParam productSaveParam){
        return productService.save(productSaveParam);
    }

    @PostMapping("update")
    public R adminUpdate(Product product){
        return productService.update(product);
    }

    @PostMapping("remove")
    public R adminRemove(Integer productId){
        return productService.remove(productId);
    }
}
